package com.chainyoung.component_tmperson.ui.adapter;

import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.appcommon.entity.HdWalletTokenTransaction;
import com.chainyoung.common.appcommon.hdwallet.ExchangeCalculator;
import com.chainyoung.common.appcommon.hdwallet.WalletServerConstant;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.chainyoung.common.constant.CommonStringConstants.URL;

public class HdWalletTokenTransactionAdapter extends BaseQuickAdapter<HdWalletTokenTransaction, BaseViewHolder> {

    private String myAddr;
    private  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public HdWalletTokenTransactionAdapter(@Nullable List<HdWalletTokenTransaction> data,String myAddr) {
        super(R.layout.layout_hdwallet_token_transaction_item, data);
        this.myAddr=myAddr;
    }


    @Override
    protected void convert(BaseViewHolder helper, final HdWalletTokenTransaction item) {
        boolean isSend=item.getFrom().equals(myAddr);
        helper.setText(R.id.tv_type,isSend?mContext.getString(R.string.tmperson_send):mContext.getString(R.string.tmperson_recieve));
        helper.setText(R.id.tv_time,sdf.format(new Date(item.getTimeStamp()*1000)));
        helper.setText(R.id.tv_num,new BigDecimal(item.getValue()).divide(ExchangeCalculator.ONE_ETHER,8, RoundingMode.DOWN).stripTrailingZeros().toPlainString());
        helper.getView(R.id.rl_transaction).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String url = String.format(WalletServerConstant.GET_TRANSACTION_BY_HASH_URL + item.getHash());
                RouterUtils.getInstance()
                        .build(RouterConstants.PATH_OTHERAPP)
                        .withString("title", url)
                        .withString(URL, url)
                        .navigation(mContext);
            }
        });

    }


}
